<template>
	<div style="display: flex; justify-content: center; width: 100%; height: 100vh;">
		<div style="margin-top: 5%;">
			<el-form ref="ruleFormRef" label-position="top" label-width="100px" :model="formregister"
				class="loginformland" :rules="rules">
				<el-form-item label="手机号" prop="phone">
					<div style="display: flex; grid-gap: 3%">
						<el-input placeholder="请输入手机号" v-model="formregister.phone" />
						<el-button type="primary" @click="registercode()" :disabled="!codestate.value">发送验证码</el-button>
					</div>
				</el-form-item>
				<el-form-item label="验证码">
					<el-input v-model="formregister.code" />
				</el-form-item>
				<el-form-item class="loginbutton">
					<el-button type="success" @click="registerForm()">确定</el-button>
					<el-button @click="registerresetForm()">取消</el-button>
				</el-form-item>
			</el-form>
		</div>
	</div>

</template>

<script setup>
	import {
		reactive,
		ref
	} from "vue";
	import {
		ElMessage
	} from "element-plus";
	import {
		useAuthStore
	} from "../../../store/auth";
	import {
		D_updatePhone
	} from "../../../api/d_user/setup";

	const loginuser = useAuthStore();

	const formregister = reactive({
		phone: "",
		code: "",
	});

	const codestate = reactive({
		value: true,
	})


	const code = reactive({
		value: '',
	})

	function registercode() {
		const min = 1000;
		const max = 9999;
		const randomCode = Math.floor(Math.random() * (max - min + 1)) + min;
		code.value = String(randomCode);
		console.log("生成的随机四位数 code：", code.value);
	}

	const registerForm = async () => {
		if (code.value != '' && formregister.code === code.value) {
			O_updatePhone(
				formregister.phone, loginuser.user.username
			).then((response) => {
				if (response.data.status === 1) {
					ElMessage({
						showClose: true,
						message: "修改成功",
						type: "success",
					})
					uni.switchTab({
						url: '/pages/o_user/setup/index'
					})
				} else {
					ElMessage.error("修改失败，请稍后重试");
				}
			})
		} else {
			ElMessage({
				showClose: true,
				message: "验证码错误",
				type: "error",
			});
		}

	};

	function registerresetForm() {
		uni.switchTab({
			url: '/pages/o_user/setup/index'
		});
	}
</script>

<style>

</style>